package com.project.JunM.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.JunM.entity.*;
import com.project.JunM.service.SCellService;
import com.project.JunM.service.SPayDetailsService;
import com.project.JunM.service.SPayService;
import com.project.common.entity.PageVo;
import com.project.xsf.entity.MManufacture;
import com.project.xsf.entity.MProcedure;
import com.project.xsf.entity.MProcedureModule;
import com.project.xsf.service.MManufactureService;
import com.project.xsf.service.MProcedureModuleService;
import com.project.xsf.service.MProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sPayDetails")
public class SPayDetailsController {

    @Autowired
    private SPayDetailsService sPayDetailsService;

    @Autowired
    private SPayService sPayService;

    @Autowired
    private SCellService sCellService;

    @Autowired
    private MProcedureModuleService mProcedureModuleService;
     @Autowired
     private MProcedureService mProcedureService;
    @Autowired
    private MManufactureService mManufactureService;

    @PostMapping("/querySPayDetailsAll.action")
    public List<SPayDetails> querySPayDetailsAll(Integer parentId){
        return sPayDetailsService.querySPayDetailsAll(parentId);
    }

    //出库登记查询（出库单）
    @PostMapping("/querySPayDetailsCheckInAll.action")
    public List<SPayDetails> querySPayDetailsCheckInAll(Integer parentId){
        System.out.println(sPayDetailsService.querySPayDetailsCheckInAll(parentId));
        return sPayDetailsService.querySPayDetailsCheckInAll(parentId);
    }

    //调度根据 产品编号 和 父级id
    @PostMapping("/updateSPayDetailsByProductId")
    public void updateSPayDetailsByProductId(Integer parentId,String sPayDetailsJson){
        System.out.println(parentId);
        SPayDetails[] sPayDetails = JSONObject.parseObject(sPayDetailsJson, SPayDetails[].class);
        for(int i=0;i<sPayDetails.length;i++){
            SPayDetails sPayDetail = sPayDetails[i];
            sPayDetail.setParentId(parentId);
            //标识符，表示已登记
            sPayDetail.setPayTag("K002-1");
            System.out.println(sPayDetail);
            sPayDetailsService.updateSPayDetailsByProductId(sPayDetail);
        }
        //更改  库存标志（STORE_TAG） K002-1: 已登记
        SPay sPay = new SPay();
        sPay.setId(parentId);
        sPay.setStoreTag("K002-1");
        sPayService.updateByPrimaryKeySelective(sPay);
    }

    //出库调度 查询
    @PostMapping("/querySPayDetailsByParentIdByProductId.action")
    public SPayDetails querySPayDetailsByParentIdByProductId(Integer parentId,String productId){

        SPayDetails sPayDetails = new SPayDetails();
        sPayDetails.setParentId(parentId);
        sPayDetails.setProductId(productId);

        return sPayDetailsService.querySPayDetailsByParentIdByProductId(sPayDetails);

    }

    //确认 调度修改 按照 产品编号
    @PostMapping("/updateSPayDetailsByProductId1")
    public void updateSPayDetailsByProductId1(Integer gatheredAmount,String productId,String registerTime,Integer parentId){

        SPayDetails sPayDetails = new SPayDetails();
        sPayDetails.setProductId(productId);
        sPayDetails.setParentId(parentId);
        SPayDetails sPayDetails1 = sPayDetailsService.selectByProductId(sPayDetails);
        //拿取 已经 出库件数
        Integer paidAmount = sPayDetails1.getPaidAmount();
        paidAmount=paidAmount==null?0:paidAmount;
        //已出库存的数量+现在要出库的数量  paidAmount+gatheredAmount
        sPayDetails1.setPaidAmount(paidAmount+gatheredAmount);
        //标识符，表示已调度
        sPayDetails1.setPayTag("K002-3");
        //执行修改
        sPayDetailsService.updateSPayDetailsByProductId(sPayDetails1);
        //查询S_Cell 以 产品编号为查询条件
        SCell sCell = sCellService.querySCellByProductById(productId);
        //当前存储量
        Integer amount = sCell.getAmount();
        amount = amount - gatheredAmount;
        sCell.setAmount(amount);
        //最大存储量
        Integer maxCapacityAmount = sCell.getMaxCapacityAmount();
        maxCapacityAmount = maxCapacityAmount+gatheredAmount;
        sCell.setMaxCapacityAmount(maxCapacityAmount);
        //修改 当前存储量 最大存储量
        sCellService.updateSCellByProductId(sCell,1);

        List<SPayDetails> sPayDetailsList = sPayDetailsService.querySPayDetails();
        boolean flag=true;
        for (int i=0;i<sPayDetailsList.size();i++){
            if(sPayDetailsList.get(i).getPayTag()==""
                    ||sPayDetailsList.get(i).getPayTag()==null){
                flag=false;
            }else if(sPayDetailsList.get(i).getPayTag().equals("K002-3")){

            }
        }
        if(flag == true) {
            Integer parentId1 = sPayDetails1.getParentId();
            SPay sPay = new SPay();
            sPay.setId(parentId1);
            sPay.setStoreTag("K002-2");
            sPay.setRegisterTime(registerTime);
            sPayService.updateByPrimaryKeySelective(sPay);
        }
        //修改生产工序物料表(M_PROCEDURE_MODULE)的已从库存领料数量
        Integer mProcedureModuleId = Integer.parseInt(sPayDetails1.getProductDescribe());
        MProcedureModule mProcedureModule = mProcedureModuleService.selectByPrimaryKey(mProcedureModuleId);
        MProcedure mProcedure = mProcedureService.selectByPrimaryKey(mProcedureModule.getParentId());
        MManufacture mManufacture = mManufactureService.selectByPrimaryKey(mProcedure.getParentId());
        List<MProcedureModule> mProcedureModules = mProcedureModuleService.queryMProcedureModuleByMManufactureId(mManufacture.getId());

        for(int i=0;i<mProcedureModules.size();i++){
            if(sPayDetails1.getProductId().equals(mProcedureModules.get(i).getProductId())){

                mProcedureModules.get(i).setRenewAmount(sPayDetails1.getPaidAmount());

                mProcedureModuleService.updateByPrimaryKeySelective(mProcedureModules.get(i));
            }
        }
    }

    @PostMapping("/queryDynamicInventoryAll.action")
    public PageVo<SPayDetails> queryDynamicInventoryAll(@RequestBody Map<String,String> map){
        return sPayDetailsService.queryDynamicInventoryAll(map);
    }


}
